package io.gitee.hchaojie.day03.tetris;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;

/**
 * 控制游戏开始，暂停，结束的面板
 * 
 * @author hchaojie
 *
 */
public class ControlPane extends VBox {
	private StackPane previewPane;
	
	private DoubleProperty squareSizeProperty = new SimpleDoubleProperty();

    public ControlPane(GameController gameController) {
        setPadding(new Insets(20, 20, 20, 20));
        setSpacing(10);
        
        previewPane = new StackPane();
        previewPane.setAlignment(Pos.TOP_LEFT);
        VBox.setVgrow(previewPane, Priority.ALWAYS);
        
        squareSizeProperty.bind(this.widthProperty().divide(6));

        gameController.nextBrickProperty().addListener(
        	(o, oldBrick, brick) -> {
        		showPreviewBrick(brick);
        	}
		);

        Button btnStart = new Button("开始");
        btnStart.setMaxWidth(Double.MAX_VALUE);
        btnStart.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                gameController.start();
            }
        });

        Button btnStop = new Button("停止");
        btnStop.setMaxWidth(Double.MAX_VALUE);
        btnStop.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                gameController.stop();
            }
        });

        Button btnPause = new Button("暂停");
        btnPause.setMaxWidth(Double.MAX_VALUE);

        StringBinding stringBinding = Bindings.createStringBinding(() -> {
			boolean isPaused = gameController.pausedProperty().get();
			return isPaused ? "继续" : "暂停";
		}, gameController.pausedProperty());

        btnPause.textProperty().bind(stringBinding);

        btnPause.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if (gameController.pausedProperty().get()) {
                    gameController.play();
                } else {
                    gameController.pause();
                }
            }
        });

        Separator sp = new Separator();

        Label lblInfo = new Label("使用↑↓←→移动和旋转砖块");
        lblInfo.setMaxHeight(Double.MAX_VALUE);
        
        getChildren().addAll(previewPane, sp, lblInfo, btnStart, btnPause, btnStop);
    }
    
    public void showPreviewBrick(Brick brick) {
    	previewPane.getChildren().clear();
		
		if (brick == null) return;
		
		int[][] matrix = brick.getMatrix();
		
		double size = this.squareSizeProperty.get();
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				if (matrix[i][j] == 1) {
					Rectangle square = brick.createRectangle();
					square.setWidth(size);
					square.setHeight(size);
					
					square.setTranslateX(j * size);
					square.setTranslateY(i * size);
					
					previewPane.getChildren().add(square);
				}
			}
		}
    }
}
